﻿using GreenXi.Model;
using GreenXi.Pages.Power;
using GreenXi.ViewModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using static GreenXi.App;

namespace GreenXi.Pages.Navig
{
    /// <summary>
    /// UCMenuItem.xaml 的交互逻辑
    /// </summary>
    public partial class UCMenuItem : System.Windows.Controls.UserControl
    {
        MainWindow context;
        public UCMenuItem(ItemMenu itemMenu, MainWindow _context)
        {
            InitializeComponent();
            this.DataContext = new UCMenuItemViewModel();
            context = _context;
            ExpanderMenu.Visibility = itemMenu.SubItems == null ? Visibility.Collapsed : Visibility.Visible;
            ListViewItemMenu.Visibility = itemMenu.SubItems == null ? Visibility.Visible : Visibility.Collapsed;
            this.DataContext = itemMenu;
        }

        private void ListViewMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //var aa = new ((SubItem)((System.Windows.Controls.ListView)sender).SelectedItem).Screen;

            context.SwitchScreen(((SubItem)((System.Windows.Controls.ListView)sender).SelectedItem).Screen);

            if (e.AddedItems.Count > 0)
            {

                object selectedItem = e.AddedItems[0];
                SubItem subItem = (SubItem)selectedItem;
                if (subItem.Name == "注销账号")
                {
                    ConfirmDialog confirmDialog = new ConfirmDialog("确定要注销登录吗?");
                    if (confirmDialog.ShowDialog() == true)
                    {
                        CurrentUser.Clean();
                        new LoginView().Show();
                        context.Close();
                    }
                    else
                    {
                        ListViewMenu.SelectedItem = ListViewMenu.Items.CurrentItem;
                    }
                }
                //context.SwitchScreen(subItem.Screen);
            }
        }

        private void ListViewMenu_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && e.ClickCount == 1)
            {
                SubItem subItem = (SubItem)ListViewMenu.SelectedItem;
                if (subItem != null)
                {
                    if (subItem.Name == "注销账号")
                    {
                        ConfirmDialog confirmDialog = new ConfirmDialog("确定要注销登录吗?");
                        if (confirmDialog.ShowDialog() == true)
                        {
                            CurrentUser.Clean();
                            new LoginView().Show();
                            context.Close();
                        }
                        else
                        {
                            ListViewMenu.SelectedItem = ListViewMenu.Items.CurrentItem;
                        }
                    }

                    context.SwitchScreen(subItem.Screen);
                }
            }
        }

        private void ListViewItemMenu_Selected(object sender, RoutedEventArgs e)
        {
            context.SwitchScreen(new PressureCurve());
        }
    }
}
